package com.elder.business.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.*;
import com.elder.business.mapper.*;
import com.elder.business.service.IApartmentDictService;
import com.elder.business.service.IApartmentRoomInfoService;
import com.elder.common.core.domain.AjaxResult;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.enums.ApartmentType;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.StringNewUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 公寓房间信息Service业务层处理
 * 
 * @author elder
 * @date 2023-11-26
 */
@Service
public class ApartmentDictServiceImpl implements IApartmentDictService
{
    @Autowired
    private ApartmentDictMapper apartmentDictMapper;

    @Override
    public JSONObject getAllDictInfo() {
        List<ApartmentDict> dictList = apartmentDictMapper.getApartmentDictAllInfo();
        JSONObject object = new JSONObject();
        JSONArray result = new JSONArray();
        Map<Integer, List<ApartmentDict>> dictMap = dictList.stream().filter(rel -> Objects.nonNull(rel.getSuperCode())).collect(Collectors.groupingBy(ApartmentDict::getSuperCode, Collectors.toList()));
        dictMap.forEach((k,v)->{
            JSONObject obj = new JSONObject();
            obj.put("superCode",k);
            obj.put("dictInfo",v);
            result.add(obj);
        });
        object.put("data",result);
        return object;
    }

    @Override
    public TableDataInfo getDictInfoBySuperCode(Integer superCode) {
        List<ApartmentDict> dictList = apartmentDictMapper.getApartmentDictInfo(superCode);
        List<JSONObject> collect = dictList.stream().map(v -> {
            JSONObject obj = new JSONObject();
            obj.put("Id", StringNewUtils.long2Str(v.getId()));
            obj.put("code", v.getCode());
            obj.put("superCode", v.getSuperCode());
            obj.put("name", v.getName());
            return obj;
        }).collect(Collectors.toList());
        TableDataInfo result = new TableDataInfo();
        result.setCode(200);
        result.setMsg("success");
        result.setRows(collect);
        return result;
    }

    @Override
    public JSONObject getDictGroupList() {
//        List<ApartmentDict> dictInfoList = apartmentDictMapper.getApartmentDictInfoList();
        List<ApartmentDict> apartmentDictInfo = apartmentDictMapper.getApartmentDictInfo(1);
//        Set<Integer> superCodeList = dictInfoList.stream().distinct().map(ApartmentDict::getSuperCode).collect(Collectors.toSet());
//        Map<Integer, String> superCodeMap =
//                apartmentDictMapper.getApartmentDictSuperCodeName(superCodeList.stream().collect(Collectors.toList()))
//                .stream().collect(Collectors.toMap(ApartmentDict::getCode, ApartmentDict::getName));
        JSONObject result = new JSONObject();
        JSONArray array = new JSONArray();
        if (CollectionUtils.isNotEmpty(apartmentDictInfo)) {
//            Map<Integer, List<ApartmentDict>> dictList = apartmentDictInfo.stream().collect(Collectors.groupingBy(ApartmentDict::getSuperCode, Collectors.toList()));
            Map<Integer, String> codeMap = apartmentDictInfo.stream().collect(Collectors.toMap(ApartmentDict::getCode, ApartmentDict::getName));
            codeMap.forEach((k, v) -> {
                JSONObject obj = new JSONObject();
                obj.put("code", k);
                obj.put("name", v);
                array.add(obj);
            });
        }
        result.put("code", 200);
        result.put("roomType",array);
        return result;
    }

    @Override
    public AjaxResult updateDictInfo(ApartmentDict apartmentDict) {
        //字典code是不被允许修改
        apartmentDictMapper.updateApartmentDict(apartmentDict.getId(),apartmentDict.getName());
        return AjaxResult.success();
    }
}
